package com.fankui.servicecms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fankui.commonutils.R;
import com.fankui.servicecms.entity.CmsBanner;
import com.fankui.servicecms.service.CmsBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author 范奎
 * @since 2023-11-30
 */
@Api(description = "banner后台管理")
@RestController
@RequestMapping("/cmsservice/banner-admin")
public class CmsBannerAdminController {
    @Autowired
    private CmsBannerService bannerService;

    @ApiOperation(value = "逻辑删除banner")
    @DeleteMapping("remove/{id}")
    public R removeBanner(@ApiParam(name = "id", value = "bannerID", required = true) @PathVariable String id) {
        boolean flag = bannerService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "条件查询分页banner数据")
    @PostMapping("pageBannerCondition/{current}/{size}")
    public R pageBannerCondition(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
                                  @ApiParam(name = "size", value = "每页记录数", required = true) @PathVariable long size) {
        Page<CmsBanner> pageBanner = new Page<>(current, size);
        QueryWrapper<CmsBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        bannerService.page(pageBanner, wrapper);
        long total = pageBanner.getTotal();
        List<CmsBanner> records = pageBanner.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);
    }

    @ApiOperation(value = "添加banner")
    @PostMapping("addBanner")
    public R addBanner(@ApiParam(name = "banner", value = "添加banner对象", required = true) @RequestBody CmsBanner banner) {
        boolean flag = bannerService.save(banner);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据id查询banner信息")
    @GetMapping("getBanner/{id}")
    public R getBanner(@ApiParam(name = "id", value = "bannerID", required = true) @PathVariable String id) {
        CmsBanner banner = bannerService.getById(id);
        return R.ok().data("banner", banner);
    }

    @ApiOperation(value = "修改banner")
    @PostMapping("updateBanner")
    public R updateBanner(@ApiParam(name = "banner", value = "修改banner对象", required = true) @RequestBody CmsBanner banner) {
        boolean flag = bannerService.updateById(banner);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

